package com.share.PACManager;

import java.util.ArrayList;
import java.util.List;

import com.share.PACManager.R;
import com.share.PACManager.model.NoticeModel;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

public class SearchNoticeActivity extends BindActivity {

	private ListView search_result_list;
	private BaseAdapter listAdapter;
	private View button_search;
	
	private EditText notice_subject;
	private EditText notice_time;
	private EditText notice_time1;
	
	private View btn_back;
	private View btn_search;
	private View filter_layout;
	
	private View mask_layout;
	
	private List<NoticeModel>  mNotices;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.search_notice_layout);
        
        mNotices = new ArrayList<NoticeModel>();
        listAdapter = new CompanyAdapter();
        
        filter_layout = findViewById(R.id.id_filter_layout);
        
        btn_back = findViewById(R.id.id_btn_back);
        btn_back.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				finish();
			}        	
        });
        
        mask_layout = findViewById(R.id.id_mask_layout);
        btn_search = findViewById(R.id.id_btn_search);
        btn_search.setSelected(true);
        btn_search.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				if(filter_layout.getVisibility() == View.VISIBLE)
				{
					filter_layout.setVisibility(View.GONE);
					mask_layout.setVisibility(View.GONE);
					btn_search.setSelected(false);					
				}
				else
				{
					filter_layout.setVisibility(View.VISIBLE);
					mask_layout.setVisibility(View.VISIBLE);
					btn_search.setSelected(true);
				}
			}        	
        });
        
        notice_subject = (EditText) findViewById(R.id.id_notice_subject);
        notice_time = (EditText) findViewById(R.id.id_notice_time);
        notice_time1 = (EditText) findViewById(R.id.id_notice_time1);
    	
        button_search = findViewById(R.id.id_button_search);
        button_search.setOnClickListener(onClickSearch);

        View button_clear = findViewById(R.id.id_btn_clear);
        button_clear.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				notice_subject.setText("");
				notice_time.setText("");
				notice_time1.setText("");
			}        	
        });
        
        search_result_list = (ListView) findViewById(R.id.id_search_result_list);
        search_result_list.setAdapter(listAdapter);
        search_result_list.setOnItemClickListener(new OnItemClickListener(){

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {

				Intent intent = new Intent();
				intent.setClass(SearchNoticeActivity.this, ViewNoticeActivity.class);
				intent.putExtra("notice_info", mNotices.get(arg2));
				
				startActivity(intent);
			}     	
        });
    }
    
    @Override
	public void onBackPressed() {
		if(!btn_search.isSelected())
			super.onBackPressed();
		else
			btn_search.performClick();
	}
    
    private OnClickListener onClickSearch = new OnClickListener(){

		@Override
		public void onClick(View arg0) {

			hideInputMethod();		
			mNotices.clear();			
			NoticeModel notice = new NoticeModel();			
			notice.NoticeTitle = notice_subject.getText().toString().trim();		
			notice.NoticeCreateTime = notice_time.getText().toString().trim();
			notice.NoticeEffectiveTime = notice_time1.getText().toString().trim();
			mGlobal.mDatabaseHelper.queryNotices(mNotices, notice);
			listAdapter.notifyDataSetChanged();
			btn_search.performClick();
		}    	
    };
    
   
    private class CompanyAdapter extends BaseAdapter{

		@Override
		public int getCount() {
			return mNotices.size();
		}

		@Override
		public Object getItem(int position) {
			return mNotices.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			if(convertView == null)
			{
				LayoutInflater inflater=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				convertView = inflater.inflate(R.layout.notice_list_item, null, false);
			}
			TextView people_num = (TextView)convertView.findViewById(R.id.id_people_num);
			people_num.setText("" + (getCount() - position));

			NoticeModel notice = (NoticeModel) getItem(position);
			TextView notice_content = (TextView) convertView.findViewById(R.id.id_notice_content);
			TextView notice_type = (TextView) convertView.findViewById(R.id.id_notice_type);
			TextView notice_time = (TextView) convertView.findViewById(R.id.id_notice_time);
			
			notice_content.setText(Html.fromHtml(notice.NoticeContent));
			notice_type.setText(notice.NoticeTitle);
			notice_time.setText(notice.NoticeCreateTime);
			
			if(position % 2 == 0)
			{
				convertView.setBackgroundColor(Color.TRANSPARENT);
				notice_content.setBackgroundColor(Color.TRANSPARENT);
			}
			else
			{
				convertView.setBackgroundColor(Color.parseColor("#55CCCCCC"));
				notice_content.setBackgroundColor(Color.TRANSPARENT);
			}
			return convertView;
		}    	
    };
     
 	private void hideInputMethod() 
	{		
		InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);		
		View v = getCurrentFocus();
		if(v == null)
			imm.hideSoftInputFromWindow(null, 0);
		else
			imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
	}
	
    public void showInputMethod(View view) {
    	 InputMethodManager im = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    	 im.showSoftInput(view, 0);
    }
}